package com.dgut.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForOrder;
import com.dgut.pojo.OrderFlag;
import com.dgut.pojo.ServiceProvider;
import com.dgut.pojo.receive.CheckReceive;
import com.dgut.pojo.receive.ReceiveId;
import com.dgut.service.OrderFlagService;
import com.dgut.service.OrderGrabbingService;
import com.dgut.service.ProcessVoucherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import com.dgut.service.ServiceProviderService;
import com.dgut.utils.PageRequestInfo;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@Api("ServiceProviderController")
@SuppressWarnings({"all"})
@RestController
public class ServiceProviderController {

    @Resource
    private ServiceProviderService serviceProviderService;
    @Resource
    private OrderGrabbingService orderGrabbingService;
    @Resource
    private OrderFlagService orderFlagService;
    @Resource
    private ProcessVoucherService processVoucherService;

    /**
     * start-zwb
     */
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    //分页显示待审核的服务商
    @ApiOperation("分页显示待审核的服务商，返回ServiceProviderList")
    @PostMapping("/getUnderRiviewServiceProviderByPage")
    public Object getUnderRiviewServiceProviderByPage(@ApiParam(value = "需要PageNum和PageSize的封装对象")
                                           @RequestBody PageRequestInfo pageRequestInfo){
        return serviceProviderService.getUnderRiviewServiceProviderByPage(pageRequestInfo);
    }

    //分页显示正常和冻结的服务商
    @ApiOperation("分页显示正常和冻结的服务商，返回ServiceProviderList")
    @PostMapping("/getServiceProviderByPage")
    public Object getServiceProviderByPage(@ApiParam(value = "需要PageNum和PageSize的封装对象")
                                                    @RequestBody PageRequestInfo pageRequestInfo){
        return serviceProviderService.getServiceProviderByPage(pageRequestInfo);
    }

    //分页显示禁用的服务商
    @ApiOperation("分页显示禁用的服务商，返回ServiceProviderList")
    @PostMapping("/getForbidServiceProviderByPage")
    public Object getForbidServiceProviderByPage(@ApiParam(value = "需要PageNum和PageSize的封装对象")
                                           @RequestBody PageRequestInfo pageRequestInfo){
        return serviceProviderService.getForbidServiceProviderByPage(pageRequestInfo);
    }

    //分页显示可正常提供服务的服务商
    @ApiOperation("分页显示可正常提供服务的服务商，返回ServiceProviderList")
    @PostMapping("/getNormalServiceProviderByPage")
    public Object getNormalServiceProviderByPage(@ApiParam(value = "需要PageNum和PageSize的封装对象")
                                           @RequestBody PageRequestInfo pageRequestInfo){
        return serviceProviderService.getNormalServiceProviderByPage(pageRequestInfo);
    }

    @ApiOperation("服务商模糊查询，返回QualificationCertificate")
    @GetMapping("/selectServiceProviderByName")
    public Object selectServiceProviderListByName(@ApiParam(value = "服务商名称",required = false)
                                                           @RequestParam(required = false) String name){
        return serviceProviderService.selectServiceProviderListByName(name);
    }

    @ApiOperation("根据id查询服务商，返回ServiceProvider")
    @GetMapping("/selectServiceProviderById")
    public Object selectServiceProviderById(@ApiParam(value = "用户id",required = true)
                                            @RequestParam(required = true) int id){
        return serviceProviderService.selectServiceProviderById(id);
    }

    @ApiOperation("冻结服务商")
    @PostMapping("/blockServiceProvider")
    public Object blockServiceProvider(@ApiParam(value = "用户id",required = true)
                                        @RequestParam(required = true) int id){
        return serviceProviderService.blockServiceProvider(id);
    }

    @ApiOperation("解冻服务商")
    @PostMapping("/unBlockServiceProvider")
    public Object unBlockServiceProvider(@ApiParam(value = "用户id",required = true)
                                             @RequestParam(required = true) int id){
        return serviceProviderService.unBlockServiceProvider(id);
    }

    @ApiOperation("根据id删除服务商")
    @PostMapping("/deleteServiceProviderById")
    public Object deleteServiceProviderById(@ApiParam(value = "用户id",required = true)
                                                @RequestParam(required = true) int id){
        return serviceProviderService.deleteServiceProviderById(id);
    }

    @ApiOperation("审核通过资质")
    @PostMapping("/checkServiceProvider")
    public Object checkServiceProvider(@ApiParam(value = "服务商id,资质等级",required = true)
                                            @RequestBody CheckReceive checkReceive){
        return serviceProviderService.checkServiceProvider(checkReceive);
    }

    @ApiOperation("服务商审核不通过")
    @PostMapping("/unCheckServiceProvider")
    public Object unCheckServiceProvider(@ApiParam(value = "服务商id",required = true)
                                             @RequestParam(required = true) int id){
        return serviceProviderService.unCheckServiceProvider(id);
    }

    @ApiOperation("设为优质服务商")
    @PostMapping("/setQualityOrganization")
    public Object setQualityOrganization(@ApiParam(value = "用户id",required = true)
                                                @RequestParam(required = true) int id){
        return serviceProviderService.setQualityOrganization(id);
    }

    @ApiOperation("取消优质服务商")
    @PostMapping("/unSetQualityOrganization")
    public Object unSetQualityOrganization(@ApiParam(value = "用户id",required = true)
                                         @RequestParam(required = true) int id){
        return serviceProviderService.unSetQualityOrganization(id);
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    @ApiOperation(value = "服务商查看账户基本信息",notes = "")
    @GetMapping("/checkPersonalInformation")
    public Object checkPersonalInformation(@ApiParam(value = "传服务商的id",required = true)
                                           @RequestParam Integer id){
        return serviceProviderService.CheckPersonalInformation(id);
    }

    @ApiOperation(value = "服务商修改账户基本信息",notes = "")
    @PostMapping("/UpdatePersonalInformation")
    public Object UpdatePersonalInformation(@ApiParam(value = "传服务商实体类",required = true)
                                            @RequestBody ServiceProvider serviceProvider){
        return serviceProviderService.UpdatePersonalInformation(serviceProvider);
    }
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("获取服务商信息")
    @GetMapping("/getMyInfoServiceProvider")
    public Object getServiceProvider(){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        ServiceProvider serviceProvider = serviceProviderService.selectServiceProviderById(Integer.valueOf((String) StpUtil.getLoginId()));
        resultMap.put("serviceProvider",serviceProvider);
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("更新服务商信息")
    @PostMapping("/updateServiceProvider")
    public Object updateServiceProvider(@ApiParam(value = "服务商信息",required = true)
                                            @RequestBody ServiceProvider serviceProvider) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(serviceProviderService.updateServiceProvide(serviceProvider)){
            resultMap.put("result", true);
            resultMap.put("message", "修改成功！");
        }else{
            resultMap.put("result", false);
            resultMap.put("message", "修改失败！");
        }
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商撤销抢单")
    @PostMapping("/deleteOrderGrabbing")
    public Object deleteOrderGrabbing(@ApiParam(value = "抢单id",required = true) ReceiveId receiveId ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Integer id = receiveId.getId();
        if(id != null){
            if(orderGrabbingService.deleteOrderGrabbing(id)){
                resultMap.put("result", true);
                resultMap.put("message", "撤销成功！");
            }else{
                resultMap.put("result", false);
                resultMap.put("message", "撤销失败！");
            }
        }else{
            resultMap.put("result", false);
            resultMap.put("message", "撤销失败！");
        }
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商未付款订单列表")
    @PostMapping("/nonPaymentOrderList")
    public Object nonPaymentOrderList(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        resultMap.put("nonPaymentOrderList",orderFlagService.nonPaymentOrderListForServiceProvider(pageRequestInfo,Integer.valueOf((String) StpUtil.getLoginId())));
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商待处理订单列表")
    @PostMapping("/untreatedOrderList")
    public Object untreatedOrderList(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        resultMap.put("untreatedOrderPage",orderFlagService.untreatedOrderListForServiceProvider(pageRequestInfo,Integer.valueOf((String) StpUtil.getLoginId())));
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商已处理订单列表")
    @PostMapping("/treatedOrderList")
    public Object treatedOrderList(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        resultMap.put("treatedOrderList",orderFlagService.treatedOrderListForServiceProvider(pageRequestInfo,Integer.valueOf((String) StpUtil.getLoginId())));
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商已完成订单列表")
    @PostMapping("/finishedOrderList")
    public Object failOrderGrabbing(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        resultMap.put("treatedOrderList",orderFlagService.finishedOrderListForServiceProvider(pageRequestInfo,Integer.valueOf((String) StpUtil.getLoginId())));
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商获取订单信息")
    @PostMapping("/getOrderForServiceProvide")
    public Object getOrderForServiceProvide(@ApiParam(value = "订单id",required = true) ReceiveId receiveId ){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Integer id = receiveId.getId();
        if(id != null){
            OrderFlag orderFlag = orderFlagService.getOrderForServiceProvide(id);
            if(orderFlag != null){
                resultMap.put("result", true);
                resultMap.put("orderFlag", orderFlag);
                resultMap.put("message", "获取成功！");
            }else{
                resultMap.put("result", false);
                resultMap.put("message", "获取失败！");
            }
        }else{
            resultMap.put("result", false);
            resultMap.put("message", "获取失败！");
        }
        return resultMap;
    }

    @SaCheckLogin
    @SaCheckRole(SelectRole.SERVICE_PROVIDER)
    @ApiOperation("服务商处理订单")
    @PostMapping("/processOrderFlag")
    public Object processOrderFlag(@ApiParam(value = "订单id",required = true) @RequestBody ReceiveId receiveId,@ApiParam(value = "凭证url",required = true)@RequestParam String url){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Integer id = receiveId.getId();
        if(id != null && url != null && !"".equals(url)){
            if(orderFlagService.changeOrderState(id, StateForOrder.WAITING_FOR_REVIEW)){
                processVoucherService.insertProcessVoucher(id,url);
                resultMap.put("result", true);
                resultMap.put("message", "处理成功！");
                /**
                 * 订单状态改变通知;
                 */
            }else{
                resultMap.put("result", false);
                resultMap.put("message", "处理失败！");
            }
        }else{
            resultMap.put("result", false);
            resultMap.put("message", "处理失败！");
        }
        return resultMap;
    }



    /**
     * end-hwf
     */

    /**
     * start-hqq
     */
    /**
     * 查看优质服务商
     * @param pageRequestInfo
     * @return
     */
    @ApiOperation("查看优质服务商")
    @PostMapping("/getQualityServiceProvider")
    public Object getQualityServiceProvider(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo ){
        return serviceProviderService.getQualityServiceProvider(pageRequestInfo);
    }
    @ApiOperation("通过地点查看优质服务商")
    @PostMapping("/getQualityServiceProviderByLocation")
    public Object getQualityServiceProviderByLocation(@ApiParam(value = "分页信息",required = true) PageRequestInfo pageRequestInfo,
                                                      @ApiParam(value = "服务商办公地点",required = true) @RequestParam String location){
        return serviceProviderService.getQualityServiceProviderByLocation(pageRequestInfo,location);
    }
    /**
     * end-hqq
     */
}
